namespace ETestIT.DataAccess
{
    using System;
    using System.Collections.Generic;
    using Domain.Entities;
    using Domain.Repositories;
    using Repository;

    public class ResumeRepositoryImpl : IResumeRepository
    {
        public IList<Resume> GetAll()
        {
            throw new NotImplementedException();
        }

        public void Add(Resume resume)
        {
            const string query = @"INSERT INTO Resumes
                                               (Id, Name, Content, ContentType, Version)
                                         VALUES
                                               (@Id
                                               ,@Name
                                               ,@Content
                                               ,@ContentType
                                               ,@Version)";

            resume.Id = Guid.NewGuid();

            new FluentCommand<string>(query)
                .AddGuid("Id", resume.Id)
                .AddString("Name", resume.Name)
                .AddString("ContentType", resume.ContentType)
                .AddInt("Version", resume.Version)
                .Add("Content", resume.Content)
                .AsNonQuery();
        }

        public void Update(Resume resume)
        {
            throw new NotImplementedException();
        }

        public void Delete(Guid resumeId)
        {
            new FluentCommand<Resume>(@"DELETE FROM Resumes WHERE Id = @Id")
                .AddGuid("Id", resumeId)
                .AsNonQuery();
        }

        public Resume GetById(Guid resumeId)
        {
            const string query = @"SELECT Id
                                          ,Name
                                          ,Content
                                          ,ContentType
                                          ,Version  
                                      FROM Resumes
                                    WHERE Id = @Id";

            return new FluentCommand<Resume>(query)
                .AddGuid("Id", resumeId)
                .SetMap(ResumeMap)
                .AsSingle();
        }

        public IList<Resume> GetAllByCandidateId(Guid candidateId)
        {
            const string query = @"SELECT R.* 
                                        FROM	
	                                        CandidateResumes CR INNER JOIN Resumes R ON CR.IdResume = R.Id
                                        WHERE CR.IdCandidate = @candidateId";

            return new FluentCommand<Resume>(query)
                .AddGuid("candidateId", candidateId)
                .SetMap(ResumeMap)
                .AsList();
        }

        private Resume ResumeMap(DataReader reader)
        {
            return new Resume
                       {
                           Id = reader.GetGuid("Id"),
                           Content = reader.GetValue<byte[]>("Content"),
                           Name = reader.GetString("Name"),
                           ContentType = reader.GetString("ContentType"),
                           Version = reader.GetInt("Version")
                       };
        }
    }
}